package com.zxw.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zxw.common.api.ApiResult;
import com.zxw.common.api.PageResult;
import com.zxw.common.constant.ApiConst;
import com.zxw.common.entity.article.echarts.EchartsVisitRankBarVO;
import com.zxw.common.entity.gateway.LogEntity;
import com.zxw.common.entity.gateway.LogListQuery;
import com.zxw.common.entity.gateway.LogQuery;
import com.zxw.common.entity.gateway.LogVO;
import com.zxw.common.entity.gateway.count.CountLogEchartsVO;
import com.zxw.common.enums.ResultCode;
import com.zxw.user.service.LogService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 访问日志记录信息接口
 * </p>
 *
 * @author zhanxuewei
 * @since 2022-10-10
 */
@Api(tags = "访问日志记录信息接口", description = "LogController")
@RestController
@RequestMapping(ApiConst.API_PREFIX + "/user/log")
public class LogController {

    @Autowired
    private LogService logService;

    @PostMapping("/save")
    public ApiResult<ResultCode> save(@RequestBody LogQuery logQuery) {
        logService.save(logQuery);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @GetMapping("/pageList")
    public ApiResult<PageResult<LogVO>> list(LogListQuery logListQuery) {
        IPage<LogVO> ipage = logService.pageList(logListQuery);
        return ApiResult.success(PageResult.restPage(ipage, ipage.getRecords()));
    }

    @PostMapping("/saveBatch")
    public ApiResult<ResultCode> saveBatch(@RequestBody List<LogQuery> list) {
        logService.saveBatch(list);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @PostMapping("/update")
    public ApiResult<ResultCode> update(@RequestBody LogQuery logQuery) {
        logService.updateById(logQuery);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @PostMapping("/deletes")
    public ApiResult<ResultCode> deletes(@RequestBody List<String> ids) {
        logService.removeByIds(ids);
        return ApiResult.success(ResultCode.SUCCESS);
    }

    @GetMapping("/detail/{id}")
    public ApiResult<LogVO> detail(@PathVariable("id") String id) {
        LogEntity logEntity = logService.getById(id);
        LogVO logVO = new LogVO();
        BeanUtils.copyProperties(logEntity, logVO);
        return ApiResult.success(logVO);
    }

    @GetMapping("/countLogEcharts")
    public ApiResult<CountLogEchartsVO> countLogEcharts() {
        CountLogEchartsVO result = logService.countLogEcharts();
        return ApiResult.success(result);
    }

    @GetMapping("/echartsVisitRankBar")
    public ApiResult<EchartsVisitRankBarVO> echartsVisitRankBar() {
        EchartsVisitRankBarVO echartsVisitRankBarVO = logService.echartsVisitRankBar();
        return ApiResult.success(echartsVisitRankBarVO);
    }

}

